
#include "oled.hpp"

#include <U8g2lib.h>

#include "app_config.hpp"
#include "image_bitmap.hpp"


// 采用全屏缓冲模式，12864 OLED占用1k RAM，应该还行
// 省掉一大堆nextPage 循环，简化代码结构
static U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE, /* clock=*/PIN_SCL, /* data=*/PIN_SDA);


static const ImageInfo IMAGE_LIST[] = {
    {image::logo, 128, 22},
    {image::hi, 128, 64},
    {image::error, 128, 64},
    {image::sick, 128, 64},
    {image::angry, 128, 64},
    {image::yun, 128, 64},
    {image::love, 128, 64},
    {image::dowhat, 128, 64},
    {image::rain, 64, 64},
    {image::sun, 64, 64},
    {image::cloud, 64, 64},
    {image::wifi, 64, 64},
    {image::timeimage, 64, 64},
    {image::ipip, 64, 64},
};


void init_oled() {
    u8g2.begin();
}


void draw_image(image_index ii, size_t x, size_t y) {
    auto iii = static_cast<u8>(ii);
    auto w = IMAGE_LIST[iii].w;
    auto h = IMAGE_LIST[iii].h;
    auto *ptr = IMAGE_LIST[iii].bitmap_ptr;

    if (w < 128 || h < 64) {
        // 如果要显示的图片大小为12864，即全屏刷新，不需要清屏，
        // 也不用clearBuffer
        u8g2.clearBuffer();
    }

    u8g2.drawXBMP(x, y, w, h, ptr);
    u8g2.sendBuffer();
}


void draw_need_net() {
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.drawXBMP(0, 0, 64, 64, image::wifi);
    u8g2.drawStr(64, 20, "IP:");
    u8g2.drawStr(64, 40, "192.168.4.1");
    u8g2.drawStr(64, 60, "Need NET");
    u8g2.sendBuffer();
}


void draw_time(int hour, int minute) {
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_ncenB14_tr);
    // timeClient.update();
    u8g2.drawXBMP(0, 0, 64, 64, image::timeimage);
    // 获取当前时间
    // 显示时间到 OLED
    // String timeToDisplay = String(hour) + ":" + String(minute);
    u8g2.drawStr(64, 30, "TIME");
    u8g2.setCursor(64, 50);
    u8g2.print(hour);
    u8g2.print(':');
    u8g2.print(minute);
    u8g2.sendBuffer();
}


void draw_logo() {
    u8g2.clearBuffer();
    u8g2.drawXBMP(0, (64 / 2 - 22 / 2), 128, 22, image::logo);
    u8g2.sendBuffer();
}


void draw_wifi_control(const String &title, const Printable &ip) {
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_ncenB08_tr);
    u8g2.drawXBMP(0, 0, 64, 64, image::ipip);
    u8g2.drawStr(64, 10, title.c_str());
    u8g2.drawStr(64, 27, "EDA-Robot");
    u8g2.setCursor(64, 43);
    u8g2.print(ip);
    u8g2.drawStr(64, 60, "WIFI CTRL");
    u8g2.sendBuffer();
}


void draw_weather(const String &weather, const String &temp, const String &humidity) {
    image_index ii = image_index::cloud;
    if(weather.endsWith("雨")) {
        ii = image_index::rain;
    }
    else if(weather == "晴") {
        ii = image_index::sun;
    }

    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_ncenB08_tr);                                       // 配置字体
    u8g2.drawXBMP(0, 0, 64, 64, IMAGE_LIST[static_cast<u8>(ii)].bitmap_ptr);  // 展示图片
    // 温度
    u8g2.setCursor(64, 20);
    u8g2.print("Temp");
    u8g2.setCursor(64, 30);
    u8g2.print(temp.c_str());
    u8g2.print(" C");
    // 湿度
    u8g2.setCursor(64, 50);
    u8g2.print("Humidity");
    u8g2.setCursor(64, 60);
    u8g2.print(humidity.c_str());
    u8g2.print(" %");
    u8g2.sendBuffer();
}
